package com.zyy.smartcity1129.house;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.android.material.tabs.TabLayout;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;
import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.HouseInfo;
import com.zyy.smartcity1129.home.House1Fragment;
import com.zyy.smartcity1129.util.BannerAd;
import com.zyy.smartcity1129.util.Http;
import com.zyy.smartcity1129.util.RvAd;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: HouseFragment$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 2/12/2021$
 * @Version: 1.0
 */
public class HouseFragment extends BaseFragment {
    private Banner banner;
    private SearchView search;
    private TabLayout tab;
    private RecyclerView rvMyOrder;
    private ArrayList<String> type = new ArrayList<>(Arrays.asList("二手", "租房", "楼盘", "中介"));
    public static HouseInfo.RowsBean house;

    @Override
    protected int layId() {
        return R.layout.f_hd;
    }

    @Override
    protected String title() {
        return "找房子";
    }

    @Override
    protected boolean back() {
        return true;
    }

    @Override
    protected Boolean bottom() {
        return null;
    }

    @Override
    protected void init(View iv) {
        initView(iv);
        th(() -> {
            List<HouseInfo.RowsBean> list = Http.getInstance().get("/prod-api/api/house/housing/list", null, HouseInfo.class, 1).getRows();
            List<HouseInfo.RowsBean> mList = new ArrayList<>();
            List<Object> banners = new ArrayList<>();
            for (int i = 0; i < 4; i++) {
                banners.add(list.get(i).getPic());
            }
            mList.addAll(list.stream().filter(a -> a.getHouseType().equals(type.get(0))).collect(Collectors.toList()));
            RvAd<HouseInfo.RowsBean> rvAd = new RvAd<HouseInfo.RowsBean>(mList, R.layout.rv_base) {
                private void initView(View iv) {
                    ig = (ImageView) iv.findViewById(R.id.ig);
                    tv1 = (TextView) iv.findViewById(R.id.tv1);
                    tv2 = (TextView) iv.findViewById(R.id.tv2);
                    tv3 = (TextView) iv.findViewById(R.id.tv3);
                    tv4 = (TextView) iv.findViewById(R.id.tv4);
                    tv5 = (TextView) iv.findViewById(R.id.tv5);
                }

                private TextView tv5;
                private TextView tv4;
                private TextView tv3;
                private TextView tv2;
                private TextView tv1;
                private ImageView ig;
                @Override
                protected void initR(View vi, HouseInfo.RowsBean rowsBean, int p) {
                    initView(vi);
                    Glide.with(ig.getContext()).load(Http.base + rowsBean.getPic()).into(ig);
                    tv1.setText(rowsBean.getSourceName());
                    tv2.setText("房源面积："+rowsBean.getAreaSize()+"平方米");
                    tv3.setText("价格："+rowsBean.getPrice());
                    tv4.setText(rowsBean.getDescription());
                    tv5.setVisibility(View.GONE);
                    vi.setOnClickListener(view->{
                        house=rowsBean;
                        add(new House1Fragment());
                    });
                }
            };
            ui(()->{
                tab.removeAllTabs();
                for (String s : type) {
                    tab.addTab(tab.newTab().setText(s));
                }
                banner.setAdapter(new BannerAd(banners)).addBannerLifecycleObserver(this).setIndicator(new CircleIndicator(getContext())).start();
                rvMyOrder.setLayoutManager(new LinearLayoutManager(getContext()));
                rvMyOrder.setAdapter(rvAd);
                tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                    @Override
                    public void onTabSelected(TabLayout.Tab tab) {
                        mList.clear();
                        mList.addAll(list.stream().filter(a -> a.getHouseType().equals(tab.getText())).collect(Collectors.toList()));
                        rvAd.notifyDataSetChanged();
                    }

                    @Override
                    public void onTabUnselected(TabLayout.Tab tab) {

                    }

                    @Override
                    public void onTabReselected(TabLayout.Tab tab) {

                    }
                });
                search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                    @Override
                    public boolean onQueryTextSubmit(String query) {
                        mList.clear();
                        mList.addAll(list.stream().filter(a -> a.getSourceName().contains(query)).collect(Collectors.toList()));
                        rvAd.notifyDataSetChanged();
                        return true;
                    }

                    @Override
                    public boolean onQueryTextChange(String newText) {
                        return false;
                    }
                });
            });
        });

    }

    private void initView(View iv) {
        banner = (Banner) iv.findViewById(R.id.banner);
        search = (SearchView) iv.findViewById(R.id.search);
        tab = (TabLayout) iv.findViewById(R.id.tab);
        rvMyOrder = (RecyclerView) iv.findViewById(R.id.rvMyOrder);
    }
}
